package sunwul.mall.commonbase.model;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import sunwul.mall.commonbase.constant.BusinessStatusEnum;

import java.io.Serializable;

/**
 * @author sunwul
 * @date 2024/12/9 16:24
 * @description  统一响应结果
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("统一响应结果")
public class Result<T> implements Serializable {

    @ApiModelProperty("响应状态码")
    private Integer code;

    @ApiModelProperty("响应消息")
    private String msg;

    @ApiModelProperty("响应数据")
    private T data;

    public static <T> Result<T> success(T data) {
        return build(BusinessStatusEnum.SUCCESS.getCode(), BusinessStatusEnum.SUCCESS.getDesc(), data);
    }

    public static <T> Result<T> success(String msg, T data) {
        return build(BusinessStatusEnum.SUCCESS.getCode(), msg, data);
    }

    public static <T> Result<T> fail(String msg) {
        return build(BusinessStatusEnum.FAIL.getCode(), msg, null);
    }

    public static <T> Result<T> fail(BusinessStatusEnum businessStatusEnum) {
        return build(businessStatusEnum.getCode(), businessStatusEnum.getDesc(), null);
    }

    public static <T> Result<T> build(Integer code, String msg, T data) {
        return new Result<>(code, msg, data);
    }
}
